#include "benchmark.h"

void TotalBenchmarkStat( struct BenchmarkStat *bmstats , size_t bmstats_count , uint64_t *total_count , double *total_elapse , double *total_qps , double *min_delay , double *avg_delay , double *max_delay )
{
	uint64_t	_total_count ;
	struct timeval	_total_elapse ;
	double		_total_qps ;
	double		_min_delay ;
	double		_total_delay ;
	double		_max_delay ;
	size_t		i ;
	
	_total_count = 0 ;
	_total_elapse.tv_sec = 0 ;
	_total_elapse.tv_usec = 0 ;
	_total_qps = 0 ;
	_min_delay = MAXDOUBLE ;
	_total_delay = 0.00 ;
	_max_delay = 0.00 ;
	for( i = 0 ; i < bmstats_count ; i++ )
	{
		_total_count += bmstats[i].count ;
		_total_elapse.tv_sec += bmstats[i].diff_elapse.tv_sec ;
		_total_elapse.tv_usec += bmstats[i].diff_elapse.tv_usec ;
		while( _total_elapse.tv_usec > 1000000 )
		{
			_total_elapse.tv_usec -= 1000000 ;
			_total_elapse.tv_sec++;
		}
		
		_total_qps += bmstats[i].qps ;
		
		if( bmstats[i].delay < _min_delay )
			_min_delay = bmstats[i].delay ;
		_total_delay += bmstats[i].delay ;
		if( bmstats[i].delay > _max_delay )
			_max_delay = bmstats[i].delay ;
	}
	
	if( total_count )
		(*total_count) = _total_count ;
	if( total_elapse )
		(*total_elapse) = (double)(_total_elapse.tv_sec) + (double)(_total_elapse.tv_usec)/1000000 ;
	if( total_qps )
		(*total_qps) = _total_qps ;
	if( min_delay )
		(*min_delay) = _min_delay ;
	if( avg_delay )
		(*avg_delay) = _total_delay / (double)bmstats_count ;
	if( max_delay )
		(*max_delay) = _max_delay ;
	
	return;
}

